<?php
/**
 * Abstract.php
 *
 * @category 	Madoqua
 * @package 	Controller
 * @subpackage 	Action
 */

/** base class */
require_once 'Zend/Controller/Action.php';

/** zend auth */
require_once 'Zend/Auth.php';

/**
 * madoqua redirector
 *
 * (because that helper... you know the one... it sucks zebra-balls)
 */
require_once 'Madoqua/Redirector.php';

/**
 * Madoqua_Controller_Action_Abstract
 *
 * abstract base class for all controllers
 *
 * @category 	Madoqua
 * @package 	Controller
 * @subpackage 	Action
 */

class Madoqua_Controller_Action_Abstract extends Zend_Controller_Action {

    /**
     * helper to load models
     *
     * @var Madoqua_Controller_Action_Helper_ModelLoader
     */
    protected $_modelLoader;

    /**
     * helper to load forms
     *
     * @var Madoqua_Controller_Action_Helper_FormLoader
     */
    protected $_formLoader;

    /**
     * configuration
     *
     * @var Zend_Config_Xml
     */
    protected $_config;

    /**
     * the current user, only available if there is an identity
     *
     * @var bool|Zend_Db_Table_Row
     */
    protected $_user = false;

    /**
     * database connection
     *
     * @var Zend_Db_Adapter_Abstract
     */
    protected $_db;

    /**
     * session namespace
     *
     * @var Zend_Session_Namespace
     */
    protected $_session;

    /**
     * initialization
     *
     * @return void
     */
    public function init() {
        parent::init();

        $this->_db = Madoqua_Application::getDatabase();
        //database connection

        $this->_config = Madoqua_Application::getConfiguration();
        //application configuration

        $this->_modelLoader = $this->getHelper('modelLoader');
        //model loader helper

        $this->_formLoader = $this->getHelper('formLoader');

        $this->_modelLoader->load('Front_User');
        //load the user

        $this->_session = Madoqua_Application::getSession();
        //get the session

        if (Zend_Auth::getInstance()->hasIdentity()) {
            $this->_user = Zend_Auth::getInstance()->getIdentity();
        } elseif ($this->_session->user) {
            //FIXME when does this happen?
            $this->_user = unserialize($this->_session->user);
        }
        if (!empty($this->_user)) {
            $this->view->placeholder('madoqua')->user = $this->_user;
        }
        //manual deserialization to avoid class problems

        if (Zend_Auth::getInstance()->hasIdentity()) {
            $this->_user = Zend_Auth::getInstance()->getIdentity();
        }
        //set the user if it exists
        
        $this->getHelper('context');
        //get context helper :x
        //this is rather ugly
        //FIXME shouldn't this be done in Madoqua_Application?
    }

    /**
     * get the session
     *
     * @return Zend_Session_Namespace
     */
    protected function getSession()
    {
        if ($this->_session == null) {
            $this->_session = new Zend_Session_Namespace('madoqua');
            //madoqua session namespace
        }

        return $this->_session;
    }
}